<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import {ref} from 'vue'
import { updateArticleCategoryService, deleteArticleCategoryService } from '@/api/article/article.js'
import { sysCategoryListService } from "@/api/system/article.js";
import {ElMessage, ElMessageBox} from "element-plus";
import dayjs from 'dayjs'


const categorys = ref([])
//控制添加分类弹窗
const dialogVisible = ref(false)
//弹窗标题
const title = ref('')
//查询表单引用
const queryForm=ref()
//总条数
const total = ref(20)
//查询参数
const queryParams=ref({
  pageNum:1,//当前页
  pageSize:5,//每页条数
  id:'',
  categoryName:'',
  createTime:'',
  username:''
})
//修改分类数据模型
const categoryModel = ref({
  username:'',
  categoryName: '',
  categoryAlias: ''
})
//表单校验
const rules = {
  categoryName: [
    {required: true, message: '请输入分类名称', trigger: 'blur'},
  ],
  categoryAlias: [
    {required: true, message: '请输入分类别名', trigger: 'blur'},
  ]
}

//日期格式化
const formatDate=(date)=>{
  let today = new Date();
  let target = new Date(date)
  if(target.getFullYear()===today.getFullYear()
      &&target.getMonth()===today.getMonth()
      &&target.getDate()===today.getDate()){
    return dayjs(date).format('HH:mm')
  }else {
    return dayjs(date).format('YYYY-MM-DD')
  }
}

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  queryParams.value.pageSize = size
  sysArticleCategoryList()
}

//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  queryParams.value.pageNum = num
  sysArticleCategoryList()
}

//调用接口获取后台文章分类列表
const sysArticleCategoryList = async () => {
  let result = await sysCategoryListService(queryParams.value)
  categorys.value = result.data.items
  total.value=result.data.total
}
sysArticleCategoryList();

//重置查询表单
const resetQuery=()=>{
  queryForm.value.resetFields()
  sysArticleCategoryList()
}

//显示编辑弹框
const showDialog = (row) => {
  dialogVisible.value = true
  title.value = '编辑分类'
  categoryModel.value.categoryName = row.categoryName
  categoryModel.value.categoryAlias = row.categoryAlias
  categoryModel.value.username = row.user.username
  //扩展文章分类数据模型属性id
  categoryModel.value.id = row.id
}

//更新文章分类
const updateArticleCategory = async () => {
  await updateArticleCategoryService(categoryModel.value)
  ElMessage.success("修改成功")
  dialogVisible.value = false
  //刷新列表
  sysArticleCategoryList()
}

//删除文章分类
const deleteArticleCategory = (row) => {
  ElMessageBox.confirm(
      '你确定要删除该文章分类吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then( async () => {
        await deleteArticleCategoryService(row.id)
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        sysArticleCategoryList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>后台文章分类管理</span>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" inline >
      <el-form-item label="分类id：" style="width: 300px" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入分类id"></el-input>
      </el-form-item>
      <el-form-item label="分类名称：" style="width: 300px" prop="categoryName">
        <el-input v-model="queryParams.categoryName" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="用户名：" style="width: 300px" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="创建时间：" style="width: 300px" prop="createTime">
        <el-date-picker
            clearable
            v-model="queryParams.createTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
            size="default"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="sysArticleCategoryList">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章分类列表 -->
    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="分类id" align="center" prop="id"></el-table-column>
      <el-table-column label="用户名" align="center" prop="user.username"></el-table-column>
      <el-table-column label="分类名称" align="center" prop="categoryName"></el-table-column>
      <el-table-column label="分类别名" align="center" prop="categoryAlias"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="{ row }">
          <span> {{formatDate(row.createTime)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteArticleCategory(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="queryParams.pageNum"
                   v-model:page-size="queryParams.pageSize"
                   :page-sizes="[3, 5 ,10, 20]"
                   layout="jumper, total, sizes, prev, pager, next"
                   background
                   :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange"
                   style="margin-top: 20px; justify-content: flex-end" />
  </el-card>
  <!-- 修改分类弹窗 -->
  <el-dialog v-model="dialogVisible" :title="title" width="30%">
    <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="categoryModel.username" disabled minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="categoryAlias">
        <el-input v-model="categoryModel.categoryAlias" minlength="1" maxlength="15"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updateArticleCategory()">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>